#!/usr/bin/python
# -*-coding:UTF-8 -*-

# ========================
# @Time  : 2021/1/22
# @Author: Sunny
# ========================


"""
1.通过列表推导式完成下面数据类型转换
现有以下数据： list1 = ["{'a':11,'b':2}","[11,22,33,44]"]
需要转换为以下格式： list1 = [{'a':11,'b':2},[11,22,33,44]]
"""
list1 = ["{'a':11,'b':2}","[11,22,33,44]"]
list2 = [eval(i)for i in list1]
print(list2)


"""
2.使用列表推导式生成一个[0,5,10,15,20……50]的列表
"""
list3 = [ a for a in range (0,51) if a%5 == 0]
print(list3)
list10 = [z for z in range(0,51,5)]
print(list10)



"""
3.使用列表推导式生成一个[page1,page2,page3...page10]的列表
"""
list4 = [f'page{b}'for b in range(1,11)]
print(list4)



"""
4.Names=['python','java','php','c','c++','django','unittest','pytest','pymysql']
请通过列表推导式，获取字符串长度大于4的元素
提示：列表推导式可以结合三目运算符一起使用
"""
Names=['python','java','php','c','c++','django','unittest','pytest','pymysql']
new_name = [ c for c in Names if len(c)>4]
print(new_name)


"""
5.通过列表推导式和字典推导式完成下面数据转换
转换前数据
str = '
url:www.baidu.com,mobilephone:13760246701,pwd:123456
url:www.baidu.com,mobilephone:13760246702,pwd:234555
url:www.baidu.com,mobilephone:13760246703,pwd:234555
url:www.baidu.com,mobilephone:13760246704,pwd:234555
url:www.baidu.com,mobilephone:13760246705,pwd:234555
"
转换后数据
list = [{'url':'www.baidu.com','mobilephone':'13760246701','pwd':'123456'},
        {'url':'www.baidu.com','mobilephone':'13760246702','pwd':'234555'},
        {'url':'www.baidu.com','mobilephone':'13760246703','pwd':'234555'},
        {'url':'www.baidu.com','mobilephone':'13760246704','pwd':'234555'},
        {'url':'www.baidu.com','mobilephone':'13760246705','pwd':'234555'},
]

"""

str = '''
url:www.baidu.com,mobilephone:13760246701,pwd:123456
url:www.baidu.com,mobilephone:13760246702,pwd:234555
url:www.baidu.com,mobilephone:13760246703,pwd:234555
url:www.baidu.com,mobilephone:13760246704,pwd:234555
url:www.baidu.com,mobilephone:13760246705,pwd:234555
'''

l =[]
b = []
one_list = []
for x in str.split():
    for i in (x.split(',')):
        l.append(i.split(':')[0])
        b.append(i.split(':')[1])
    one_list.append(dict(zip(l,b)))
print(one_list)

# list6 = [ {y.split(':')[0]:y.split(':')[1]} for x in str.split() for y in x.split(',')]
# print(list6)

#列表推导式嵌套字典推导式
list7 = [ {y.split(':')[0]:y.split(':')[1] for y in item.split(',')} for item in str.split() ]
print(list7)




